package com.jiyici.network;

import java.util.ArrayList;

import jiyici.Constant.AuthenticateKey;
import jiyici.Constant.GetAction;

import com.jiyici.controller.DataManager;
import com.jiyici.model.User;

public class GetRequest {

    public static GetRequest createRequestForGetItem() {
        GetRequest request = new GetRequest();
        request.setAction(GetAction.GET_ITEM);
        request.addAuthenticateData();
        return request;
    }

    public int getAction() {
        return mAction;
    }

    /**
     * eg: ?xxx=yyy&xxx=yyy...
     * 
     * @return
     */
    public String getRequestString() {
        StringBuilder sb = new StringBuilder("?");
        if (mParams != null) {
            int size = mParams.size();
            for (int i = 0; i < size; i++) {
                sb.append(mParams.get(i).toString());
                if (i != size - 1)
                    sb.append("&");
            }
        }
        return sb.toString();
    }

    private int mAction;
    private ArrayList<QueryItem> mParams;

    private GetRequest() {

    }

    private void addAuthenticateData() {
        User user = DataManager.getUser();
        addQueryItem(QueryItem.createQueryItem(AuthenticateKey.USER_ID, user.id + ""));
        addQueryItem(QueryItem.createQueryItem(AuthenticateKey.TOKEN, user.token));
    }

    private void setAction(int action) {
        mAction = action;
    }

    private void addQueryItem(QueryItem item) {
        if (mParams == null)
            mParams = new ArrayList<QueryItem>();
        mParams.add(item);
    }

}
